const bodyParser = require('./common/bodyParser')
const session = require('express-session')
const express = require('express')
const cors = require('cors')
const multer = require('multer')
const multe = require('./common/multer')
const db = require("./common/mysql")
const app = express()

// session 配置
app.use(
    session({
        secret: 'jhh',
        resave: false,
        saveUninitialized: true,
    })
)

// 跨域
app.use(cors())

const storage = multer.diskStorage(multe)

const upload = multer({
    storage
})
// app.use(express.static("./index"));
app.use(express.static("./dist"));

// 图片上传接口
app.post('/upload', upload.array('file', 18), (req, res) => {
    let files = req.files
    if (files === null || files == "") return res.send("0")
    let num = ""
    for (let i in req.files) {
        num += req.files[i].filename + ','
    }
    let str = [num, session.id]
    let sql = "UPDATE `use-article` SET `img`=? WHERE `id`=?";
    console.log(num);
    db.query(sql, str, (err, results) => {
        if (err) return console.log(err.message);
        res.send('ok')
    })


})

// 上传文章
app.get('/article', bodyParser, (req, res) => {
    let query = req.query

    let id = ""
    let data = new Date();
    id += data.getMinutes() + 1, //获取当前分钟数(0-59)
        id += data.getSeconds() + 1, //获取当前秒数(0-59)
        id += data.getMilliseconds() + 1; //获取当前毫秒数(0-999) 

    // 存储时间
    let date = "";
    date += data.getFullYear() + '-', //获取完整的年份(4位,1970-????)
        date += data.getMonth() + 1 + '-', //获取当前月份(0-11,0代表1月)
        date += data.getDate();  //获取当前日(1-31)

    session.id = id

    let str = [id, query.identity, query.name, query.image, query.article, date]

    let sql = 'INSERT INTO `use-article`(`id`, `identity`, `name`, `image`, `article`, `time`) VALUES (?,?,?,?,?,?)'

    db.query(sql, str, (err, results) => {
        if (err) return console.log(err.message);
        res.send({
            data: id
        })
    })
})

// 获取文章
app.get('/articleout', (err, res) => {

    db.query("SELECT * FROM `use-article`", (err, results) => {
        if (err) {
            console.log(err.message);
            return res.send('1')
        }
        console.log(results);
        res.send(results)

    })

})

// 发送评论
app.get('/sendcomments', bodyParser, (req, res) => {
    console.log(req.query);
    let query = req.query
    let id = ""
    let str = [id, query.onid, query.identity, query.image, query.name, query.area]
    console.log(str);
    let sql = 'INSERT INTO `use-article-area`(`id`, `on`, `identity`, `image`, `name`, `area`) VALUES (?,?,?,?,?,?)';
    db.query('SELECT * FROM `use-article-area`', (err, results) => {
        str[0] = results.length + 1
        db.query(sql, str, (err, results) => {
            if (err) return console.log(err.message);
            res.send("0")
            console.log(results);
        })
    })

})

// 获取评论
app.get('/pinglun', (req, res) => {
    let query = req.query[0]

    let sql = 'SELECT * FROM `use-article-area` WHERE `on`=?'

    db.query(sql, query, (err, results) => {
        if (err) return console.log(err.message);
        if (results.length == 0) return res.send("0")
        res.send(results)
        console.log(results.length, results);
    })
})

// 登录
app.post('/admindr', bodyParser, (req, res) => {
    const body = req.body
    let number = body.number, pwd = body.pwd
    let sql = 'SELECT * FROM `use` WHERE `user`=? and `password`=?'

    db.query(sql, [number, pwd], (err, results) => {
        if (results == "") {
            return res.send(1)
        }
        else {
            console.log(results);
            res.send({
                status: 0,
                results
            })
            session.user = results
            session.islogin = true

        }
    })
})
// 判断是否登录
app.get('/user', bodyParser, (req, res) => {
    if (!session.islogin) {
        return res.send({ status: 1, msg: 'fail' })
    }
    let user = session.user
    res.send({ status: 0, msg: 'success', user })
})
// 注册
app.post('/get', bodyParser, (req, res) => {
    const body = req.body

    let name = '', user = body.number, password = body.pwd, identity = '2022', image = '1.jpg'

    for (let i in user) {
        i = parseInt(i)
        if (i > 6) {
            identity += user[i];
        }
    }
    let sql = "INSERT INTO `roots`.`use` VALUES (?,?,?,?,?)";
    name = identity
    db.query(sql, [name, user, password, identity, image], (err, results) => {
        if (err) return res.send('400');
        if (results.affectedRows === 1) {
            console.log('注册成功');
            res.end('注册成功')
        }
    })

})


app.listen(80, () => {
    console.log('express serve running at http://127.0.0.1');
})
